﻿<%@ WebHandler Language="C#" Class="update_comment" %>

using System;
using System.Web;
using Libs.Content;

public class update_comment : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private int articleID;
    private string title;
    private string name;
    private string email;
    private string address;
    private string phone;
    private string content;
    private string captcha;

    private HttpContext context;
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        this.context = context;

        ParseQuery();
        PostComment();
    }

    private void ParseQuery()
    {
        articleID = int.Parse(context.Request["aid"]);
        title = context.Request["title"];
        name = context.Request["name"];
        email = context.Request["email"];
        address = context.Request["address"];
        phone = context.Request["phone"];
        content = context.Request["content"];
        captcha = context.Request["captcha"];
    }

    private void PostComment()
    {
        if (ServerValid())
        {
            context.Response.Write("-1");
            return;
        }
        if (context.Session["Code"].ToString().ToLower() != captcha.ToLower())
        {
            context.Response.Write("-3");
            return;
        }

        //if (context.Session["LastPost_" + articleID.ToString()] != null)
        //{
        //    DateTime lastTime = Convert.ToDateTime(context.Session["LastPost_" + articleID.ToString()]);

        //    if (lastTime.Subtract(DateTime.Now).Seconds < 10)
        //    {
        //        context.Response.Write("-2");
        //        return;
        //    }
        //}

        //context.Session["LastPost_" + articleID.ToString()] = DateTime.Now;
        
        Libs.Content.NewsForum m_newsForum = new NewsForum();
        m_newsForum.NewsID = articleID;
        m_newsForum.Title = title;
        m_newsForum.FullName = name;
        m_newsForum.Email = email;
        m_newsForum.Content = content;
        m_newsForum.Header = title;
        m_newsForum.Add();
        int _ForumID = m_newsForum.ForumID;
        if (_ForumID > 0)
        {
            context.Response.Write("1");
        }
        else
        {
            context.Response.Write("-1");
        }
    }
    private bool ServerValid()
    {
        if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(content))
        {
            return true;
        }

        return false;
    }

    private long ConvertToInt64(object obj)
    {
        if (obj == null)
        {
            return 0;
        }

        long ret = 0;

        try
        {
            ret = Convert.ToInt64(obj);
        }
        catch (Exception)
        {
        }

        return ret;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}